<template>
  <!-- 核保点评规则组件 -->
  <div>
    <el-table :data="tableData" style="width: 100%" height="630px" ref="multipleTable" highlight-current-row border
      header-cell-class-name="table-header-class">
      <el-table-column label="产品分类" align='center' min-width="180">
        <template slot-scope="scope">
          <el-select v-model="scope.row.productType" placeholder="请选择" clearable :disabled="pageDisabled">
            <el-option v-for="item in productTypeOpts" :key="item.id" :label="item.dictValue" :value="item.id + ''">
            </el-option>
          </el-select>
        </template>
      </el-table-column>

      <el-table-column label="体检等级" align='center' min-width="180">

        <template slot-scope="scope">
          <el-select v-model="scope.row.gradedContent" placeholder="请选择" clearable :disabled="pageDisabled">
            <el-option v-for="item in gradedContentOpts" :key="item.id" :label="item.propertyType" :value="item.id + ''">
            </el-option>
          </el-select>
        </template>
      </el-table-column>


      <el-table-column label="核保结论" align='center' min-width="180">

        <template slot-scope="scope">
          <el-select v-model="scope.row.underwritingConclusion" placeholder="请选择" clearable :disabled="pageDisabled">
            <el-option v-for="item in underwritingConclusionOpts" :key="item.id" :label="item.dictValue"
              :value="item.id + ''">
            </el-option>
          </el-select>
        </template>
      </el-table-column>


      <el-table-column label="操作" align='center' min-width="150" fixed="right" v-if="!pageDisabled">

        <template slot-scope="scope">
          <div style="display:flex;justify-content: center;">
            <span class="handle-btn" @click="handleAdd(scope.$index, scope.row)">新增</span>
            <span class="handle-btn" @click="handleDel(scope.$index, scope.row)">删除</span>
            <span class="handle-btn" @click="handleCopy(scope.$index, scope.row)">复制</span>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹窗 -->
    <!-- 新增疾病规则弹窗 -->
    <el-dialog v-if="conditionVisible" title="配置条件" :visible.sync="conditionVisible" width="850px"
      :close-on-click-modal="false" center>
      <conditionRelation ref="conditionRelation" :tableTitle="tableTitle" source="underwrite" :logicDic="logicDic"
        @saveRelation="() => { conditionVisible = false }"></conditionRelation>
    </el-dialog>
  </div>
</template>

<script>
import conditionRelation from "./conditionRelation";
import { getOwnDictProjectApi } from "@/api/medicalLibrary/examProjectLibrary";
import constant from '@/utils/constant';
import { getUnderwritingConclusionsRuleApi, deletUnderwritingConclusionsRuleApi, getItemspropertiesApi } from "@/api/medicalLibrary/underwritingRule";
import { getCommonInfoApi } from "@/api/medicalLibrary/medicalbank";

export default {
  components: {
    conditionRelation
  },
  props: {
    itemID: {
      type: Number
    },
    logicDic: {
      type: Array,
      default: () => {
        return []
      }
    },
    pageDisabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 字典
      gradedContentOpts: [],//体检等级
      productTypeOpts: [],//产品分类
      underwritingConclusionOpts: [],//核保结论
      conclusionDic: [], //核保结论
      conclusionRetDic: [],  //核保评点
      tableData: [{}],
      ruleGroupVersionId: '',  //版本id

      // 弹窗
      conditionVisible: false,
      tableTitle: '',  //表头名称
    }
  },
  created() {
    this.getSelectOptsList();
  },
  methods: {
    getRuleDataList(itemsCode) {
      this.$store.dispatch('loading/CHANGE_LOADING', true)
      this.tableData = []
      let params = {
        id: this.itemID,
        flag: 1
      }
      getItemspropertiesApi(params).then(res => {
        this.$store.dispatch("loading/CHANGE_LOADING", false);
        if (res.resultCode == constant.SUCCESS) {
          if (res.data != [] && res.data.length > 0) {
            var propertyId = res.data[0].propertyId
            getCommonInfoApi({ id: propertyId }).then((res) => {
              if (res.resultCode === 0) {
                this.gradedContentOpts = res.data && res.data.length ? res.data : [{ propertyType: '' }]
                this.getListData(itemsCode);
              }
            }).catch((err) => {
              this.$store.dispatch("loading/CHANGE_LOADING", false);
            });
          } else {
            this.getListData(itemsCode);
          }
        }
      })
    },
    getListData(itemsCode) {
      getUnderwritingConclusionsRuleApi({ id: itemsCode }).then(res => {
        this.$store.dispatch('loading/CHANGE_LOADING', false)
        if (res.resultCode == constant.SUCCESS) {
          this.tableData = res.data || []
        }
      }).catch(err => {
        this.$store.dispatch('loading/CHANGE_LOADING', false);
      });
    },
    //获取产品分类、体检等级、核保结论的下拉框的数据
    getSelectOptsList() {
      getOwnDictProjectApi({ code: 'productType' }).then(res => {
        if (res.resultCode == constant.SUCCESS) {
          this.productTypeOpts = res.data || []
          this.productTypeOpts.forEach(item => {
            item.value = item.remark
            item.label = item.dictValue
          })
        }
      })
      // getOwnDictProjectApi({ code: 'tirads' }).then(res => {
      //   if (res.resultCode == constant.SUCCESS) {
      //     this.gradedContentOpts = res.data || []
      //     this.gradedContentOpts.forEach(item => {
      //       item.value = item.remark
      //       item.label = item.dictValue
      //     })
      //   }
      // })
      getOwnDictProjectApi({ code: 'underConclusions' }).then(res => {
        if (res.resultCode == constant.SUCCESS) {
          this.underwritingConclusionOpts = res.data || []
          this.underwritingConclusionOpts.forEach(item => {
            item.value = item.remark
            item.label = item.dictValue
          })
        }
      })
    },
    /* ----列表操作按钮 */
    // 添加
    handleAdd(index, row) {
      let obj = {
        productType: '',
        gradedContent: '',
        underwritingConclusion: '',
      }
      this.tableData.push(obj)
      // this.tableData.splice(index+1,0,obj)
    },
    // 删除
    handleDel(_index, row) {
      this.$confirm(`是否删除此子项目信息?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.tableData.splice(_index, 1)
          if (row.id) {
            deletUnderwritingConclusionsRuleApi({ id: row.id }).then((res) => {
              if (res.resultCode == constant.SUCCESS) {
                this.$message.success('删除成功')
              }
            })
              .catch((err) => {
              });
          } else {
            this.$message.closeAll()
            this.$message.success('删除成功')
          }
        })
        .catch(() => { })
    },
    // 复制
    handleCopy(index, row) {
      let fields = ['productType', 'gradedContent', 'underwritingConclusion']
      let obj = {}
      fields.forEach(item => {
        obj[item] = row[item]
      })
      this.tableData.push(obj)
      // this.tableData.splice(index+1,0,obj)
    },
    // 点击子项目保存接口
    checkSaveInfo() {
      for (let one of this.tableData) {
        if (!one.productType) {
          this.$message.closeAll()
          this.$message.warning('产品分类不能为空!')
          return false;
        }
        if (!one.gradedContent) {
          this.$message.closeAll()
          this.$message.warning('体检等级不能为空!')
          return false;
        }
        if (!one.underwritingConclusion) {
          this.$message.closeAll()
          this.$message.warning('核保结论不能为空!')
          return false;
        }
      }
      return true;
    },
    // 处理保存的数据
    saveHandle() {
      let riskRuleFieds = ['productType', 'gradedContent', 'underwritingConclusion'],
        flag = true
      let filterArr = JSON.parse(JSON.stringify(this.tableData))
      filterArr.forEach(fItem => {
        let itemFlag = false
        if (this.ruleGroupVersionId) fItem.ruleGroupVersionId = this.ruleGroupVersionId
        riskRuleFieds.forEach(item => {
          if (fItem[item] || fItem[item] === 0) itemFlag = true
        })
        if (!itemFlag) flag = false
      })
      return {
        flag,
        filterArr
      }
    }
  }
}
</script>

<style lang="less" scoped>
.handle-btn {
  cursor: pointer;
  color: #409eff;
  display: inline-block;
  padding: 0 8px;
}

.result-item {
  color: #40a9fd;
  cursor: pointer;
  width: 100%;
  display: inline-block;
  height: 24px;
  line-height: 30px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.disabled-item {
  color: #606266;
  cursor: no-drop;
}

/deep/ .el-input-number__decrease,
/deep/ .el-input-number__increase {
  display: none;
}
</style>